package com.splic.product.service.impl;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.splic.common.exception.BusinessException;
import com.splic.common.utils.DateUtils;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.*;
import com.splic.product.dto.UserMotionRankingDto;
import com.splic.product.mapper.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.splic.product.service.UserMotionDataService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户运动量Service业务层处理
 * 
 * @author singe
 * @date 2022-01-04
 */
@Service
public class UserMotionDataServiceImpl implements UserMotionDataService
{
    @Autowired
    private UserMotionDataMapper userMotionDataMapper;
    @Autowired
    private UserMotionRankingCountryMapper userMotionRankingCountryMapper;
    @Autowired
    private UserMotionRankingProvinceMapper userMotionRankingProvinceMapper;
    @Autowired
    private UserMotionRankingCityMapper userMotionRankingCityMapper;
    @Autowired
    private UserMotionRankingCountyMapper userMotionRankingCountyMapper;
    @Autowired
    private AreaMapper areaMapper;
    @Autowired
    private UserMapper userMapper;


    /**
     * 查询用户运动量
     * 
     * @param id 用户运动量主键
     * @return 用户运动量
     */
    @Override
    public UserMotionData selectUserMotionDataById(Long id)
    {
        return userMotionDataMapper.selectUserMotionDataById(id);
    }

    /**
     * 查询用户运动量列表
     * 
     * @param userMotionData 用户运动量
     * @return 用户运动量
     */
    @Override
    public List<UserMotionData> selectPageList(UserMotionData userMotionData)
    {
        return userMotionDataMapper.selectUserMotionDataList(userMotionData);
    }

    /**
     * 查询用户运动量列表
     *
     * @param userMotionData 用户运动量
     * @return 用户运动量
     */
    @Override
    public List<UserMotionData> selectUserMotionDataList(UserMotionData userMotionData)
    {
        return userMotionDataMapper.selectUserMotionDataList(userMotionData);
    }

    /**
     * 新增用户运动量
     * 
     * @param userMotionData 用户运动量
     * @return 结果
     */
    @Override
    public int insert(UserMotionData userMotionData)
    {
        userMotionData.setCreateTime(DateUtils.getNowDate());
        return userMotionDataMapper.insert(userMotionData);
    }

    /**
     * 修改用户运动量
     * 
     * @param userMotionData 用户运动量
     * @return 结果
     */
    @Override
    public int update(UserMotionData userMotionData)
    {
        userMotionData.setUpdateTime(DateUtils.getNowDate());
        return userMotionDataMapper.update(userMotionData);
    }


    /**
     * 删除用户运动量信息
     * 
     * @param id 用户运动量主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id)
    {
        return userMotionDataMapper.deleteById(id);
    }
    /**
     * 修改用户运动量
     *
     * @param userMotionData 用户运动量信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveOrUpdate(UserMotionData userMotionData)
    {
        if(userMotionData.getId() != null){
            userMotionData.setUpdateTime(new Date());
            return userMotionDataMapper.update(userMotionData);
        }else{
            userMotionData.setCreateTime(new Date());
            return userMotionDataMapper.insert(userMotionData);
        }
    }

    /**
     * 获取用户运动量排名
     * @param rankingType
     * @param userId
     * @return
     */
    @Override
    public List<UserMotionRankingDto> selectUserMotionRankingList(String rankingType, Long userId) {

        Date yestedayDate = org.apache.commons.lang3.time.DateUtils.addDays(new Date(),-1);
        String yesdayStr = DateUtils.dateTime(yestedayDate);

        User user = userMapper.selectUserById(userId);
        if(user == null){
            throw new BusinessException("当前用户获取失败");
        }

        List<UserMotionRankingDto> returnList = Lists.newArrayList();
        UserMotionRankingDto dto = null;
        PageHelper.orderBy("ranking asc");

        if(StringUtils.equals("country",rankingType)){
            List<UserMotionRankingCountry> countryList = userMotionRankingCountryMapper.selectUserMotionRankingCountryListByDate(yesdayStr,null);
            List<UserMotionRankingCountry> userCountryList = userMotionRankingCountryMapper.selectUserMotionRankingCountryListByDate(yesdayStr,userId);
            if(CollectionUtils.isNotEmpty(countryList)){
                for(UserMotionRankingCountry t:countryList){
                    dto = new UserMotionRankingDto();
                    dto.setRanking(t.getRanking());
                    dto.setUserId(t.getUserId());
                    dto.setWalkStep(t.getWalkStep());
                    returnList.add(dto);
                }
            }
            if(CollectionUtils.isNotEmpty(userCountryList)){
                dto = new UserMotionRankingDto();
                dto.setRanking(userCountryList.get(0).getRanking());
                dto.setUserId(userCountryList.get(0).getUserId());
                dto.setWalkStep(userCountryList.get(0).getWalkStep());
                dto.setIsSelf(1);
                returnList.add(dto);
            }
        }else if(StringUtils.equals("province",rankingType)){
            List<UserMotionRankingProvince> provinceList = userMotionRankingProvinceMapper.selectUserMotionRankingProvinceListByDate(yesdayStr,null,user.getProvinceCode());
            List<UserMotionRankingProvince> userProvinceList = userMotionRankingProvinceMapper.selectUserMotionRankingProvinceListByDate(yesdayStr,userId,null);
            if(CollectionUtils.isNotEmpty(provinceList)){
                for(UserMotionRankingProvince t:provinceList){
                    dto = new UserMotionRankingDto();
                    dto.setRanking(t.getRanking());
                    dto.setUserId(t.getUserId());
                    dto.setWalkStep(t.getWalkStep());
                    returnList.add(dto);
                }
            }
            if(CollectionUtils.isNotEmpty(userProvinceList)){
                dto = new UserMotionRankingDto();
                dto.setRanking(userProvinceList.get(0).getRanking());
                dto.setUserId(userProvinceList.get(0).getUserId());
                dto.setWalkStep(userProvinceList.get(0).getWalkStep());
                dto.setIsSelf(1);
                returnList.add(dto);
            }
        }else if(StringUtils.equals("city",rankingType)){
            List<UserMotionRankingCity> cityList = userMotionRankingCityMapper.selectUserMotionRankingCityListByDate(yesdayStr,null,user.getCityCode());
            List<UserMotionRankingCity> userCityList = userMotionRankingCityMapper.selectUserMotionRankingCityListByDate(yesdayStr,userId,null);
            if(CollectionUtils.isNotEmpty(cityList)){
                for(UserMotionRankingCity t:cityList){
                    dto = new UserMotionRankingDto();
                    dto.setRanking(t.getRanking());
                    dto.setUserId(t.getUserId());
                    dto.setWalkStep(t.getWalkStep());
                    returnList.add(dto);
                }
            }
            if(CollectionUtils.isNotEmpty(userCityList)){
                dto = new UserMotionRankingDto();
                dto.setRanking(userCityList.get(0).getRanking());
                dto.setUserId(userCityList.get(0).getUserId());
                dto.setWalkStep(userCityList.get(0).getWalkStep());
                dto.setIsSelf(1);
                returnList.add(dto);
            }
        }else if(StringUtils.equals("county",rankingType)){
            List<UserMotionRankingCounty> countyList = userMotionRankingCountyMapper.selectUserMotionRankingCountyListByDate(yesdayStr,null,user.getCountyCode());
            List<UserMotionRankingCounty> userCountyList = userMotionRankingCountyMapper.selectUserMotionRankingCountyListByDate(yesdayStr,userId,null);
            if(CollectionUtils.isNotEmpty(countyList)){
                for(UserMotionRankingCounty t:countyList){
                    dto = new UserMotionRankingDto();
                    dto.setRanking(t.getRanking());
                    dto.setUserId(t.getUserId());
                    dto.setWalkStep(t.getWalkStep());
                    returnList.add(dto);
                }
            }
            if(CollectionUtils.isNotEmpty(userCountyList)){
                dto = new UserMotionRankingDto();
                dto.setRanking(userCountyList.get(0).getRanking());
                dto.setUserId(userCountyList.get(0).getUserId());
                dto.setWalkStep(userCountyList.get(0).getWalkStep());
                dto.setIsSelf(1);
                returnList.add(dto);
            }
        }else if(StringUtils.equals("friend",rankingType)){
            List<UserMotionRankingCounty> countyList = null;//userMotionRankingCountyMapper.selectUserMotionRankingCountyListByDate(yesdayStr,null);
            List<UserMotionRankingCounty> userCountyList = null;//userMotionRankingCountyMapper.selectUserMotionRankingCountyListByDate(yesdayStr,userId);
            if(CollectionUtils.isNotEmpty(countyList)){
                for(UserMotionRankingCounty t:countyList){
                    dto = new UserMotionRankingDto();
                    dto.setRanking(t.getRanking());
                    dto.setUserId(t.getUserId());
                    dto.setWalkStep(t.getWalkStep());
                    returnList.add(dto);
                }
            }
            if(CollectionUtils.isNotEmpty(userCountyList)){
                dto = new UserMotionRankingDto();
                dto.setRanking(userCountyList.get(0).getRanking());
                dto.setUserId(userCountyList.get(0).getUserId());
                dto.setWalkStep(userCountyList.get(0).getWalkStep());
                dto.setIsSelf(1);
                returnList.add(dto);
            }
        }
        return returnList;
    }



    /**
     * 用户运动量排名统计
     */
    @Override
    public void motionRankingStats() {
        Date yestedayDate = org.apache.commons.lang3.time.DateUtils.addDays(new Date(),-1);
        String yesdayStr = DateUtils.dateTime(yestedayDate);

        Date currentDate = new Date();
        //查询国家排名
        for(int i = 1;i<10000;i++){
            PageHelper.startPage(i,100,"walk_step desc");
            List<UserMotionData> userMotionDataList = userMotionDataMapper.selectCountryPageList(yesdayStr);
            if(CollectionUtils.isEmpty(userMotionDataList)){
                break;
            }
            UserMotionRankingCountry country = null;
            int index = 1;
            for(UserMotionData userMotionData:userMotionDataList){
                country = new UserMotionRankingCountry();
                country.setDate(yestedayDate);
                country.setUserId(userMotionData.getUserId());
                country.setRanking((i-1)*100+index);
                country.setCreateTime(currentDate);
                country.setWalkStep(userMotionData.getWalkStep());
                userMotionRankingCountryMapper.insert(country);
                index++;
            }
        }


        List<Area> provinceAreaList = areaMapper.selectProvinceList();
        for (Area area:provinceAreaList){
            //查询省份排名
            for(int i = 1;i<10000;i++){
                PageHelper.startPage(i,100,"walk_step desc");
                List<UserMotionData> userMotionDataList = userMotionDataMapper.selectProvincePageList(area.getCode(),yesdayStr);
                if(CollectionUtils.isEmpty(userMotionDataList)){
                    break;
                }
                UserMotionRankingProvince province = null;
                int index = 1;
                for(UserMotionData userMotionData:userMotionDataList){
                    province = new UserMotionRankingProvince();
                    province.setDate(yestedayDate);
                    province.setUserId(userMotionData.getUserId());
                    province.setRanking((i-1)*100+index);
                    province.setCreateTime(currentDate);
                    province.setProvinceCode(area.getCode());
                    province.setWalkStep(userMotionData.getWalkStep());
                    userMotionRankingProvinceMapper.insert(province);
                    index++;
                }
            }
        }


        List<Area> cityAreaList = areaMapper.selectCityList();
        for (Area area:cityAreaList){
            //查询城市排名
            for (int i = 1; i < 10000; i++) {
                PageHelper.startPage(i, 100, "walk_step desc");
                List<UserMotionData> userMotionDataList = userMotionDataMapper.selectCityPageList(area.getCode(), yesdayStr);
                if (CollectionUtils.isEmpty(userMotionDataList)) {
                    break;
                }

                UserMotionRankingCity city = null;
                int index = 1;
                for (UserMotionData userMotionData : userMotionDataList) {
                    city = new UserMotionRankingCity();
                    city.setDate(yestedayDate);
                    city.setUserId(userMotionData.getUserId());
                    city.setRanking((i-1) * 100 + index);
                    city.setCreateTime(currentDate);
                    city.setCityCode(area.getCode());
                    city.setWalkStep(userMotionData.getWalkStep());
                    userMotionRankingCityMapper.insert(city);
                    index++;
                }
            }
        }

        List<Area> countyAreaList = areaMapper.selectCountyList();
        for (Area area:countyAreaList) {
            //查询县排名
            for (int i = 1; i < 10000; i++) {
                PageHelper.startPage(i, 100, "walk_step desc");
                List<UserMotionData> userMotionDataList = userMotionDataMapper.selectCountyPageList(area.getCode(), yesdayStr);
                if (CollectionUtils.isEmpty(userMotionDataList)) {
                    break;
                }

                UserMotionRankingCounty county = null;
                int index = 1;
                for (UserMotionData userMotionData : userMotionDataList) {
                    county = new UserMotionRankingCounty();
                    county.setDate(yestedayDate);
                    county.setUserId(userMotionData.getUserId());
                    county.setRanking((i-1) * 100 + index);
                    county.setCreateTime(currentDate);
                    county.setCountyCode(area.getCode());
                    county.setWalkStep(userMotionData.getWalkStep());
                    userMotionRankingCountyMapper.insert(county);
                    index++;
                }
            }
        }

    }
}
